package com.zenithmind.chat.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zenithmind.chat.pojo.entity.ChatGroupAssociation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 群组关联映射器 - 遵循单一职责原则
 * 只负责群组关联的数据访问操作
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Mapper
public interface ChatGroupAssociationMapper extends BaseMapper<ChatGroupAssociation> {

    /**
     * 根据群组ID查询关联信息
     * 
     * @param groupId 群组ID
     * @return 关联信息列表
     */
    @Select("SELECT * FROM chat_group_association WHERE group_id = #{groupId} AND deleted = 0")
    List<ChatGroupAssociation> selectByGroupId(@Param("groupId") String groupId);

    /**
     * 根据关联类型查询关联信息
     * 
     * @param associationType 关联类型
     * @return 关联信息列表
     */
    @Select("SELECT * FROM chat_group_association WHERE association_type = #{associationType} AND deleted = 0")
    List<ChatGroupAssociation> selectByAssociationType(@Param("associationType") String associationType);

    /**
     * 根据关联ID查询关联信息
     * 
     * @param associationId 关联ID
     * @return 关联信息列表
     */
    @Select("SELECT * FROM chat_group_association WHERE association_id = #{associationId} AND deleted = 0")
    List<ChatGroupAssociation> selectByAssociationId(@Param("associationId") String associationId);

    /**
     * 查询群组的特定类型关联
     * 
     * @param groupId 群组ID
     * @param associationType 关联类型
     * @return 关联信息列表
     */
    @Select("SELECT * FROM chat_group_association WHERE group_id = #{groupId} AND association_type = #{associationType} AND deleted = 0")
    List<ChatGroupAssociation> selectByGroupIdAndType(@Param("groupId") String groupId, 
                                                      @Param("associationType") String associationType);

    /**
     * 检查关联是否存在
     * 
     * @param groupId 群组ID
     * @param associationType 关联类型
     * @param associationId 关联ID
     * @return 是否存在
     */
    @Select("SELECT COUNT(*) FROM chat_group_association WHERE group_id = #{groupId} AND association_type = #{associationType} AND association_id = #{associationId} AND deleted = 0")
    int checkAssociationExists(@Param("groupId") String groupId, 
                              @Param("associationType") String associationType,
                              @Param("associationId") String associationId);
}
